/*
 *  @(#)PersonEditor.java    0.3 3/05/2009
 */
package org.miao.example.controller;

import java.beans.PropertyEditorSupport;
import org.miao.example.domain.Person;

/**
 * Person的PropertyEditor
 * @author Miao
 * @version 0.3
 * @since 0.3
 */
public class PersonEditor extends PropertyEditorSupport{

    @Override
    public void setAsText(String text) {
        if (text == null || text.trim().length() == 0) {
            throw new IllegalArgumentException("text is null");
        }
        setValue(parseString(text));
    }

    /**
     * 将字符串解析成Person对象
     * @param text 待解析字符串
     * @return 解析好的对象
     * @since 0.3
     */
    private Object parseString(String text) {
        String[] params = text.split(",");
        Person person = new Person();
        person.setId(Long.parseLong(params[0]));
        person.setName(params[1]);
        person.setAge(Integer.parseInt(params[2]));
        person.setDescription(params[3]);
        return person;
    }
}
